<template>
  <div >
    <!-- form card -->
    <el-card
      v-for="(e, i) in makeData"
      :key="e.index"
      :class="e.formStyle || 'box-card'"
    >
      <el-form
        :inline="true"
        :ref="dataModel[i].formRef"
        :model="dataModel[i]"
        class="demo-form-inline"
      >
        <el-col :span="24">
          <el-col
            v-for="item in e.formComponent"
            :key="item.model"
            :span="item.span ? item.span : 5"
          >
            <el-form-item
              :label-width="item.labelWidth||'120px'"
              :prop="item.model"
              :rules="item.rules"
              :label="item.label"
              class="postInfo-container-item"
            >
              <!-- input-->
              <el-input
                v-if="item.type == 'input'"
                v-model="dataModel[i][item.model]"
                style="width: 150px"
                class="filter-item"
                @keyup.enter.native="item.keyUpEnter"
              />
              <!-- inputTextArea-->

              <el-input
                v-if="item.type == 'textArea'"
                type="textarea"
                :rows="3"
                placeholder="请输入内容"
                v-model="textarea"
              >
              </el-input>
              <!-- select-->
              <el-select
                v-if="item.type == 'select'"
                :placeholder="$t('basicEquipment.importance')"
                clearable
                style="width: 150px"
                class="filter-item"
              >
                <el-option
                  v-for="e in item.option"
                  :key="e"
                  :label="e"
                  :value="e"
                />
              </el-select>
              <!-- datepicker-->
              <el-date-picker
                v-if="item.type == 'datePicker'"
                style="width: 150px"
              />
              <!-- dateRangepicker -->
              <el-date-picker
                v-if="item.type == 'dateRangepicker'"
                type="datetimerange"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
              />
              <el-upload
                v-if="item.type == 'upload'"
                class="upload-demo"
                :limit="1"
                action="https://jsonplaceholder.typicode.com/posts/"
                :on-preview="handlePreview"
                :on-remove="handleRemove"
                :file-list="fileList"
                list-type="picture"
              >
                <el-button size="small" type="primary">点击上传</el-button>
                <div slot="tip" class="el-upload__tip">
                  只能上传jpg/png文件，且不超过500kb
                </div>
              </el-upload>
            </el-form-item>
          </el-col>
        </el-col>
      </el-form>
    </el-card>
    <!-- 提交 -->
   
    <!-- <el-col v-for="item in buttonGroups" :key="item.name" :span="item.span||2" :offset="item.index==0? 6:0">
      <el-button :type="item.style || ''" @click="enterForm" >{{item.name}}</el-button>
    </el-col> -->
  </div>
</template>

<script>
export default {
  mounted() {
    console.log(this);
  },
  props: ["dataForm"],
  data() {
    return {
      buttonGroups:[],
      dataModel: this.dataForm,
      // dataModel: this.dataModel.formComponent,
      // listQuery: this.dataModel.formComponent,
      formRules: this.dataForm.formRules,
      downloadLoading: false,
      fileList: [],
      makeData: this.dataForm,
      //  makeData:[{
      //    formRef:'formFirst',//表单ref
      //    formModel:this.dataModel.formModel,//表单model
      //   formComponent:this.dataModel.formComponent,
      //    formName:'表单A'//表单标题
      //   },{
      //   formRef:'formSecond',
      //    formModel:this.dataModel.formModel,
      //     formComponent:this.dataModel.formComponent,
      //    formName:'表单B'
      //   },{
      //     formRef:'formThird',
      //    formModel:this.dataModel.formModel,
      //     formComponent:this.dataModel.formComponent,
      //    formName:'表单C'
      //   }],
    };
  },
  created() {

      console.log(this.$store.state,"state");
      console.log(this.$route.path,"router");
      this.buttonGroups=this.getButtonGroupsByUrl(this.$route.path)
       console.log(this.getButtonGroupsByUrl(this.$route.path));
  },
  methods: {
    getButtonGroupsByUrl(router){
        let getButtonGroups=JSON.parse(localStorage.getItem("buttonGroups"));
        return getButtonGroups[router]
    },
    enterForm() {
      //动态表单提交
      console.log(this.$refs);
      let arrForm = []; //哪些表单需要做校验
      let arrModel = []; //表单的值
      let newArr = []; //承接promise的返回结果
      let _self = this;
      this.makeData.forEach((item, index) => {
        //将有权限的表单做校验等等
       

        checkForm(item.formRef); //校验
        arrModel.push(item); //需要校验的表单的值
      });
      function checkForm(arrName) {
        //根据form表单的ref，动态生成promise，再对其坐表单校验，都通过了再去做处理
        console.log(_self.$refs[arrName], "-----");
        var result = new Promise(function (resolve, reject) {
          _self.$refs[arrName][0].validate((valid) => {
            if (valid) {
              resolve();
            } else {
              reject();
            }
          });
        });
        newArr.push(result); //push 得到promise的结果
      }
      Promise.all(newArr)
        .then(function () {
          //都通过了
          console.log(arrModel);
          console.log("恭喜，表单全部验证通过");
        })
        .catch(function () {
          console.log(arrModel);
          console.log("err");
        });
    },
  },
};
</script>
<style scoped>
.box-card {
  border: 0px;
  background-color: #fff;
  overflow: hidden;
  color: #303133;
  box-shadow: 0 0px 0px 0 rgba(0, 0, 0, 0.1);
}
.box-margin-card {
  border: 0px;
  background-color: #fff;
  overflow: hidden;
  color: #303133;
  box-shadow: 0 0px 0px 0 rgba(0, 0, 0, 0.1);
}

</style>